package com.ccp.dev.workflow.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.GlobalType;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.workflow.bpmutil.ServiceUtil;
import com.ccp.dev.workflow.dao.TaskDao;
import com.ccp.dev.workflow.model.BpmProTransTo;
import com.ccp.dev.workflow.model.BpmProTransToAssignee;
import com.ccp.dev.workflow.model.bpm.ProcessTask;
import com.ccp.dev.workflow.service.BpmProTransToService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * ClassDescribe:加签 控制器类
 * @author :jiayueqiao
 * Date: 2019-10-16
 * Since:1
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT +UrlConstants.PROJECT_MODULE_WORKFLOW+"/bpmProTransTo")
@Action(ownermodel=SysAuditModelType.PROCESS_MANAGEMENT)
public class BpmProTransToController extends SysBaseController {

    @Resource
    private BpmProTransToService bpmProTransToService;
    @Resource
    private TaskDao taskDao;

    /**
     * 加签流转事宜页面
     * @param model 视图模型对象
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "查看加签流转事宜页面",detail = "查看加签流转事宜页面")
    public String viewList(Model model) {
        //获取消息通知类型
        Map handlersMap = ServiceUtil.getHandlerMap();
        model.addAttribute("handlersMap", handlersMap);
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
        model.addAttribute("curUserId", sysUser.getUserId());
        return getAutoView();
    }

    /**
     * 获取加签流转的流程分类
     * @return ResultData
     */
    @RequestMapping("/getFlowType")
    @ResponseBody
    public ResultData getFlowType() {
        String assignee =  ContextUtil.getCurrentUserId();
        List<GlobalType> processRunList = bpmProTransToService.getByCatKeyForToMatter(assignee);
        return getResultDataSuccess("query.success",processRunList);
    }


    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "获取流转事宜列表数据",detail="获取流转事宜列表数据<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request,true);
        String nodePath = RequestUtil.getString(request, "nodePath");
        if(StringUtil.isNotEmpty(nodePath)){
            queryFilter.addFilter("nodePath",nodePath + "%");
        }
        queryFilter.addFilter("exceptDefStatus", 3);
        String userId = ContextUtil.getCurrentUserId();
        queryFilter.addFilter("createUserId", userId);
        List<BpmProTransTo> list;
        try {
            list = bpmProTransToService.queryList(queryFilter);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("query.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("fl", true);
        return getResultDataSuccess("query.success", queryFilter.getPageBean().getTotalCount(), list);
    }

    /**
     * 查看流转人页面
     * @return String
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/showAssignee")
    @Action(description = "查看流转人页面",detail = "查看流转人页面")
    public String viewShowAssignee(HttpServletRequest request,Model model) {
        String taskId = RequestUtil.getString(request,"taskId");
        model.addAttribute("taskId",taskId);
        //获取消息通知类型
        Map handlersMap = ServiceUtil.getHandlerMap();
        model.addAttribute("handlersMap", handlersMap);
        return getAutoView();
    }

    /**
     * 查看流转人
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/showAssignee")
    @Action(description = "获取流转人数据",detail = "获取流转人数据<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData showAssignee(HttpServletRequest request){
        String taskId  = RequestUtil.getString(request, "taskId");
        BpmProTransTo bpmProTransTo = bpmProTransToService.getByTaskId(taskId);
        if(BeanUtils.isEmpty(bpmProTransTo)){
            return getResultDataError("task.transTo.is.end");
        }
        try{
            List<BpmProTransToAssignee> list = bpmProTransToService.getAssignee(taskId, bpmProTransTo.getAssignee());
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess("query.success", list);
        }catch (Exception e){
            e.printStackTrace();
        }
        SysAuditThreadLocalHolder.putParamerter("fl", false);
        return getResultDataError("query.failure");
    }

    /**
     * 取消流转任务
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/cancel")
    @Action(description = "取消流转任务",detail="取消流转任务,任务id为${taskId}<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData cancel(HttpServletRequest request){
        String taskId = RequestUtil.getString(request, "taskId");
        String opinion = RequestUtil.getString(request, "opinion");
        String userId = RequestUtil.getString(request, "userId");
        String informType = RequestUtil.getString(request, "informType");
        SysAuditThreadLocalHolder.putParamerter("taskId", taskId);
        try{
            ProcessTask processTask = taskDao.getTaskByParentIdAndUser(taskId, userId);
            if(processTask==null){
                SysAuditThreadLocalHolder.putParamerter("fl", false);
                return getResultDataError("task.transTo.already.finished");
            }else{
                bpmProTransToService.cancel(processTask, opinion, informType);
                SysAuditThreadLocalHolder.putParamerter("fl", true);
                return getResultDataSuccess("task.transTo.cancel.success");
            }
        }catch(Exception e){
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("task.transTo.cancel.failure");
        }
    }

    /**
     * 添加流转人
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/add")
    @Action(description = "添加流转人",detail="为任务添加流转人，任务id为${taskId}，添加的流转人id包括${userIds}，操作<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData add(HttpServletRequest request){
        String userIds = request.getParameter("cmpIds");
        SysAuditThreadLocalHolder.putParamerter("userIds", userIds);
        try{
            String taskId = RequestUtil.getString(request, "taskId");
            SysAuditThreadLocalHolder.putParamerter("taskId", taskId);
            String opinion = request.getParameter("opinion");
            String informType=RequestUtil.getString(request, "informType");
            BpmProTransTo bpmProTransTo = bpmProTransToService.getByTaskId(taskId);
            if(BeanUtils.isEmpty(bpmProTransTo)){
                SysAuditThreadLocalHolder.putParamerter("fl", false);
                return getResultDataError("task.transTo.is.end");
            }else{
                bpmProTransToService.addTransTo(bpmProTransTo, taskId, opinion, userIds, informType);
                SysAuditThreadLocalHolder.putParamerter("fl", true);
                return getResultDataSuccess("task.transTo.addOperator.success");
            }
        }catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("task.transTo.addOperator.failure");
        }
    }
}
